package com.drama.outtake.util;

import com.drama.outtake.MyException;
import com.drama.outtake.domain.enums.Gender;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

/**
 * @Author : 李先均
 * @Date 2023/3/5 14:24
 */
public class CustomGenderDeserializer extends JsonDeserializer<Gender> {
    @Override
    public Gender deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException {
        String value = jsonParser.getText();
        if ("男".equals(value) || "MALE".equals(value)) {
            return Gender.MALE;
        } else if ("女".equals(value) || "FEMALE".equals(value)) {
            return Gender.FEMALE;
        } else {
            throw new MyException("传入的性别参数不合法");
        }
    }
}
